package com.bizteco.policelogviewer.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.management.openmbean.CompositeData;
import javax.management.openmbean.TabularData;

import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;

public class MiscUtil {
	
    /**
     * 
     * @param str
     * @param division
     * @return
     * @Note:Remove Null
     */
  	public static String[] Tokenize(String str, String division) {
	   int i = 0;
	   StringTokenizer st = new StringTokenizer( str, division);
	   int numTokens = st.countTokens();
	       
	   if( numTokens == 0) {
		   return null;
	   }
	   // System.out.println( "division=" + division + ":" + "numTokens=" + numTokens);
	       
	   String[] str_arr = new String[numTokens];
	   
	   for(i = 0; i < numTokens; i++) {
	      str_arr[i] = st.nextToken();
	      // System.out.printf( "%d=[%s]\n", i, str_arr[i]);
	   }
	   
	   return str_arr;
	}

  	/**
  	 * 
  	 * @param line
  	 * @param delim
  	 * @return
  	 * 
  	 * For Performance.
  	 */
  	public static String[] Tokenize3( String line, char delim) {
         
  		List<String> lineList = new ArrayList<String>();
         
         int pos = 0, end;
         
         while ((end = line.indexOf( delim, pos)) >= 0) {
        	lineList.add( line.substring(pos, end));
            pos = end + 1;
         }
         
         String[] lineArr = lineList.toArray(new String[lineList.size()]);
         
         return lineArr;
  	}
  	
  	/**
  	 * 
  	 * @param line
  	 * @param delim
  	 * @return
  	 * 
  	 * For Performance.
  	 */
  	public static String[] Tokenize4( String line, char delim, int columns) {
         
  		// System.out.println( "Columns=" + columns);
  		String[] lineArr = new String[columns];
  		
        int fieldnum = 0, pos = 0, end;
         
        while ((end = line.indexOf( delim, pos)) >= 0) {
           lineArr[fieldnum++] = line.substring(pos, end);
           pos = end + 1;
        }
         
        return lineArr;
  	}
  	
  	public static void setShellCenter( Display display, Shell shell) {
	    Monitor primary = display.getPrimaryMonitor();
	    Rectangle bounds = primary.getBounds();
	    Rectangle rect = shell.getBounds();
	    
	    int x = bounds.x + (bounds.width - rect.width) / 2;
	    int y = bounds.y + (bounds.height - rect.height) / 2;
	    
	    shell.setLocation(x, y);
	}
  	
  	/**
  	 * 
  	 * @param fileName
  	 * @param data
  	 * @throws IOException 
  	 */
  	public static void writeStringToFile( String fileName, String data) throws IOException {
  		MiscUtil.writeStringToFile( new File( fileName), data);
  	}
  	
  	/**
  	 * 
  	 * @param file
  	 * @param data
  	 * @throws IOException 
  	 */
  	public static void writeStringToFile( File file, String data) throws IOException {
        FileWriter fileWriter = null;
        try {
        	fileWriter = new FileWriter( file);
        	fileWriter.write( data);
        	fileWriter.close();
        } catch (IOException ex) {
        	// Logger.getLogger( MiscUtil.class.getName()).log(Level.SEVERE, null, ex);
        	throw ex;
        } finally {
        	try {
        		fileWriter.close();
        	} catch (IOException ex) {
        		Logger.getLogger( MiscUtil.class.getName()).log(Level.SEVERE, null, ex);
        	}
        }   		
  	}
  	
  	/**
  	 * Extracts the named TabularData field from the CompositeData and converts it to a Map
  	 * which is the method used to get the typesafe user properties.
  	 */
  	public static Map getTabularMap(CompositeData cdata, String fieldName) {
  	    Map map = new HashMap();
  	    Object tabularObject = cdata.get(fieldName);
  	    if (tabularObject instanceof TabularData) {
  	        TabularData tabularData = (TabularData) tabularObject;
  	        // Collection<CompositeData> values = tabularData.values();
  	        Collection<CompositeData> values = ( Collection<CompositeData>)tabularData.values();
  	        for (CompositeData compositeData : values) {
  	            Object key = compositeData.get("key");
  	            Object value = compositeData.get("value");
  	            map.put(key, value);
  	        }
  	    }
  	    return map;
  	}  	
}
